<template>
  <view class="page container">
		<scroll-view scroll-y style="max-height: 90vh;">
			<u-form
			  :model="basicForm"
			  :rules="rules"
			  labelPosition="top"
			  ref="form"
			  labelWidth="auto"
			  :labelStyle="{
			  	fontSize: '14px',
			  	color: '#B2B2B2',
			  	lineHight: '20px'
			  }"
			>
			  <view class="basic-style">
			    <u-form-item label="中标单位" prop="biddingUnit" required>
			      <h-select-customer-input-popup
			        style="width: 100%"
			        v-model="basicForm.biddingUnit"
			        objectField="basicForm.biddingUnit"
			        commercialType="2"
			        placeholder="请选择中标单位"
							:isJudge='true'
							@onJudge='onJudge'
							suffixIcon="arrow-down-fill"
							:suffixIconStyle="{color: '#B2B2B2'}"
			        border="surround"/>
			    </u-form-item>
			    <u-form-item label="中标价(元)" prop="bidding" required>
						<h-money-input placeholder="请输入中标价" v-model="basicForm.bidding"></h-money-input>
			    </u-form-item>
			    
			    <u-form-item label="税率(%)" prop="taxRate" required style="position: relative">
						<h-number-input placeholder="请输入税率" :digit="2" :max="100" :min="0"  v-model="basicForm.taxRate"></h-number-input>
			      <span class="percent" style="position: absolute; right: 1em">%</span>
			    </u-form-item>
			    <u-form-item label="发票类型" prop="invoiceCategory" required>
			      <h-select-input-tag
			        class="w-100"
			        v-model="basicForm.invoiceCategory"
			        objectField="basicForm.invoiceCategory"
			        placeholder="请选择发票类型"
			        border="surround"
			        dictName="invoice_type"
							suffixIcon="arrow-down-fill"
							:suffixIconStyle="{color: '#B2B2B2'}"
			        @change="invoiceCategoryBlur"
			      ></h-select-input-tag>
			    </u-form-item>
			    <!-- <u-form-item label="价格执行有效期" prop="validity" required>
			      <h-select-input-tag
			        class="w-100"
			        v-model="basicForm.validity"
			        objectField="basicForm.validity"
			        placeholder="请选择价格执行有效期"
			        border="surround"
			        dictName="validity_type"
							suffixIcon="arrow-down-fill"
							:suffixIconStyle="{color: '#B2B2B2'}"
			      ></h-select-input-tag>
			    </u-form-item> -->
					<u-form-item label="是否垫资" prop="isAdvancePayment" required>
					  <h-select-input-tag
					    class="w-100"
					    v-model="basicForm.isAdvancePayment"
					    objectField="basicForm.isAdvancePayment"
					    placeholder="请选择是否垫资"
					    border="surround"
					    dictName="yes_no"
							suffixIcon="arrow-down-fill"
							:suffixIconStyle="{color: '#B2B2B2'}"
					  ></h-select-input-tag>
					</u-form-item>
					<u-form-item label="工期(供货期)" prop="duration" required>
					  <h-time-select
					    mode="date"
					    ref="remittanceDate"
					    v-model="basicForm.duration"
					    placeholder="请选择工期"
					    border="surround"
					    suffixIcon="calendar-fill"
					    :suffixIconStyle="{ color: '#B2B2B2' }"
					  ></h-time-select>
					</u-form-item>
					<u-form-item label="付款方式" prop="payType" required>
			      <!-- <u--input placeholder="请输入付款方式" v-model="basicForm.payType"></u--input> -->
						<u--textarea
						  :confirm-type="null"
						  :maxlength="1000"
							count
						  v-model="basicForm.payType"
						  placeholder="请输入付款方式"
						></u--textarea>
			    </u-form-item>
			  </view>
			</u-form>
		</scroll-view>
    
		<view class="space-x-1 mt-1" style="display: flex">
			<u-button type="primary" text="关闭" :plain="true" :loading="loading" @click="onClose"></u-button>
		  <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
		  
		</view>
  </view>
</template>

<script>
export default {
  name: 'WinTheBidForm',
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
    teamId: {
      type: String,
      default: '',
      required: false,
    },
  },
  data() {
    return {
      smallList: [],
      loading: false,
      basicForm: {
		  biddingUnit:'',
        invitationId: '',
        projectId: '',
        teamId: '',
				isAdvancePayment:'',
				duration: ''
      },
      rules: {
        biddingUnit: [{ required: true, message: '请选择中标单位!', trigger: ['change', 'blur'] }],
        bidding: [
          { type: 'number', required: true, message: '请填写中标价!', trigger: ['change', 'blur'] },
          { required: true, pattern: /^([0-9]*[1-9][0-9]*(\.[0-9]+)?|[0]+\.[0-9]*[1-9][0-9]*)$/, message: '请输入大于0的数字!', trigger: ['change','blur'] },
        ],
        invoiceCategory: [{ required: true, message: '请选择发票类型!', trigger: ['change', 'blur'] }],
        payType: [{ required: true, message: '请填写付款方式!', trigger: ['change', 'blur'] }],
        isAdvancePayment: [{ required: true, message: '请选择是否垫资!', trigger: ['change', 'blur'] }],
        duration: [{ required: true, message: '请选择工期!', trigger: ['change', 'blur'] }],
        taxRate: [
          { type: 'number', required: true, message: '请填写税率!', trigger: ['change', 'blur'] },
          { required: true, pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的税率!', trigger: ['change', 'blur'] },
        ],
        validity: [{ required: true, message: '请选择有效期!', trigger: ['change', 'blur'] }],
      },
    }
  },
  methods: {
		onJudge(){
			// 保存返回的监听，事先处理好
			uni.$on("supplierBack", res => {
				
				this.basicForm.biddingUnit = res.id
				
				// 清除监听
				uni.$off('supplierBack');
			})

			uni.navigateTo({
				url:'/pages/common/selectSupplier/selectSupplierInput'+'?isProPayContract=1&isNoJudge=1'
			})
		},
    invoiceCategoryBlur(value) {
      if (value == 0) {
        this.$set(this.basicForm, 'taxRate', 0)
      } else {
        this.$set(this.basicForm, 'taxRate', null)
      }
    },
    handleInvitationChange(material) {
      this.$set(this.basicForm, 'projectId', material.projectId) // 项目名称
      this.$set(this.basicForm, 'teamId', material.teamId) // 团队名称
    },
    reset() {
      this.$refs.form.resetFields()
    },
    async onSubmit() {
      await this.$refs.form.validate()
			let form = Object.assign({}, this.basicForm)
			// return false
      if (this.index === null) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...form,
          },
          this.index,
        ])
      } else {
        this.$emit('submit', [form, this.index])
      }
    },
    onClose() {
      this.$emit('close')
    },
  },
  watch: {
    provisionBroad: {
      handler(val) {
        if (val) {
          this.findProvisionSmallList(val)
        }
      },
      deep: true,
    },
  },
}
</script>

<style scoped></style>
